package com.example.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

import static com.example.common.ProjectConstants.*;

@Component
public class AutoInitField implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.fillStrategy(metaObject, CREATE_TIME, LocalDateTime.now());
        this.fillStrategy(metaObject, UPDATE_TIME, LocalDateTime.now());
        /*
        CREATE_USER和UPDATE_USER的修改需要作者先实现一个ThreadLocal，用来存储UserDto对象
         */
        // this.fillStrategy(metaObject, CREATE_USER, UserHolder.get().getId());
        // this.fillStrategy(metaObject, UPDATE_USER, UserHolder.get().getId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.fillStrategy(metaObject, UPDATE_TIME, LocalDateTime.now());
    }
}
